var postdata = require("../../../data/posts-data.js");
var app=getApp();

Page({
    data: {
        article: '',
        collection: '',
        articleid: '',
        isplaymusic: ''
    },
    onLoad: function (option) {
        var G=app.globalData;
        var postid = option.id;
        this.setData({
            article: postdata.postList[postid],
            articleid: postid

        });

        var article = wx.getStorageSync('article');

        if (article) {
            article = article[postid];
            this.setData({
                collection: article
            });
        } else {
            var article = {};
            article[postid] = false;
            wx.setStorageSync('article', article)
        }

     this.listenmusic();
     if(G.g_isplaymusic && postid==G.g_currentmusicid){
         this.setData({
            isplaymusic:true
         })
        
     }

    },
    listenmusic:function(){
         var that=this;
        //监听音乐启动
        wx.onBackgroundAudioPlay(function(){
            that.setData({
                isplaymusic: true
            })
         app.globalData.g_isplaymusic=true;
         app.globalData.g_currentmusicid=that.data.articleid;
        })
    //监听音乐暂停
        wx.onBackgroundAudioPause(function(){
            that.setData({
                isplaymusic:false
            })
            app.globalData.g_currentmusicid=null;
            
        })
    //监听音乐停止
    wx.onBackgroundAudioStop(function(){
         that.setData({
                isplaymusic:false
            })
    })
    },
    
    oncollectionTap: function (event) {
        var article = wx.getStorageSync('article');

        var articleid = article[this.data.articleid];
        console.log(articleid)
        articleid = !articleid;
        article[this.data.articleid] = articleid;

        // wx.setStorageSync('article',article);
        // this.setData({
        //     collection:articleid
        // });
        // wx.showToast({
        //     title:articleid ? '收藏成功':'取消收藏',
        //     icon: 'success',
        //     duration: 1000
        // });
        var that = this;
        wx.showModal({
            title: '提示',
            content: '确定要收藏?',
            confirmText: articleid ? '收藏' : '取消收藏',
            success: function (res) {
                if (res.confirm) {
                    wx.setStorageSync('article', article);
                    that.setData({
                        collection: articleid
                    });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        });

    },
    onshareTap: function (event) {
        // wx.removeStorageSync('article');
        wx.showActionSheet({
            itemList: ['分享给好友', '分享到朋友圈', '分享到QQ', '分享到微博'],
            itemColor: '#FF7F00',
            success: function (res) {
                console.log(res)
                wx.showModal({
                    title: '分享',
                    content: '分享到QQ'
                });
            },
            error: function (res) {

            }

        });
    },
    onMusicTap: function (event) {
        var musicstatus = this.data.isplaymusic;
        var musicid = this.data.articleid;
        var musicdata = postdata.postList[musicid];
        if (musicstatus) {
            wx.pauseBackgroundAudio();
            this.setData({
                isplaymusic: false
            });
        } else {
            wx.playBackgroundAudio({
                dataUrl: musicdata.music.url,
                title: musicdata.music.title,
                coverImgUrl: '/images/xuwei.jpg',
            });
            this.setData({
                isplaymusic: true
            });


        }

    },
    onMusicpauseTap: function (event) {


        console.log(1);

    }

})